package cn.com.gf.cmp.am.monitor.trade;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
 
public class TradeSourceContext {
	
	private long serialNo = 0L;
 
	private String SELECT_SQL ;
	 
	private String branch_no;
	
	
	public TradeSourceContext(String branch_no) {
		super();
		this.branch_no = branch_no;
	}

	public long getSerialNo(JdbcTemplate jdbcTemplate,String tradeKafkaTable) {
		
		//serialNo=0，取当天最大的serial_no为初始值
		if(serialNo==0){ 
			 SELECT_SQL="select max(serial_no) as SERIAL_NO from "+tradeKafkaTable+" where  branch_no="+branch_no+" and l_date="+DateTime.now().toString("yyyyMMdd");
			 Map<String,Object> result=jdbcTemplate.queryForMap(SELECT_SQL);
			 
			 if(result!=null &&!result.isEmpty()){
				 try{
					 Object o=result.get("SERIAL_NO"); 
					 serialNo=((BigDecimal)(o!=null?o:0L)).longValue();
				    }catch(Exception e){ 
				 }
			 }		 
		}
		return serialNo;
	}

	public void setSerialNo(long serialNo) {
		this.serialNo = serialNo;
	}

	//private 
}
